package com.kkb.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.JsonObject;
import com.kkb.common.utils.StringUtils;
import com.kkb.domain.AjaxResult;
import com.kkb.dto.TbGoodsAndTbProductDto;
import com.kkb.page.PageDomain;
import com.kkb.page.TableDataInfo;
import com.kkb.page.TableSupport;
import com.kkb.pojo.*;
import com.kkb.service.*;
import com.kkb.vo.TbGoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品
 */
@Controller
@RequestMapping("/TbGoods")
public class TbGoodsController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private TbGoodsService tbGoodsService;
    @Autowired
    private TbProductService tbProductService;
    @Autowired
    private TbShopService tbShopService;
    @Autowired
    private TbBrandService tbBrandService;
    @Autowired
    private TemplateService templateService;

    @Autowired
    private TbImageService tbImageService;

    @RequestMapping("/goods")
    public String showCategory() {

        return "tbgoods/tbgoods";
    }

    //商品分类列表
    @ResponseBody
    @RequestMapping("/getGoodsList")
    public String getGoodsList(){
//        List<TbGoods> goods = tbGoodsService.getGoodslist();
//        return goods;
        List<TbProduct> getlist = tbProductService.getlist();
        for (TbProduct product : getlist) {
            System.out.println(product);
//            List<TbGoods> goods = tbGoodsService.getGoodslist(product.getProductId());
//            product.setTbGoodsList(goods);
        }
        return "" ;
    }

    /**
     * 商品列表查询
     * @return
     */
    @ResponseBody
    @RequestMapping("/getGoodsVoList")
    public TableDataInfo getGoodsVoList(){
        startPage();
        List<TbGoodsVO> goodsVoList = tbGoodsService.getGoodsVoList();
        return getDataTable(goodsVoList);
    }
    @RequestMapping("/add/{id}")
    public String addGoods(@PathVariable("id") Integer parentId, ModelMap mmap){
        // 所有商铺
        mmap.put("tbShops",  tbShopService.getTbShopAll());
        // 所有品牌
        mmap.put("tbBrands",  tbBrandService.queryBrands());
        // 所有分类
        mmap.put("tbCategorys", categoryService.getCategory());
//        categoryService.selectCategoryByLevel(1);
        mmap.put("category", categoryService.selectCategoryById(parentId));
        //  所有模板
        mmap.put("tbTemplates", templateService.getTemplate());
        return "tbgoods/addProduct";
    }

    //商品分类列表
    @ResponseBody
    @RequestMapping("/addTbGoods")
    public AjaxResult addTbGoods(TbGoodsAndTbProductDto tbGoodsAndTbProductDto){

        System.out.println(tbGoodsAndTbProductDto.toString());
        return AjaxResult.success();
    }

    /**
     * 设置请求分页数据
     */
    private void startPage() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            PageHelper.startPage(pageNum, pageSize);
        }
    }
    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    protected TableDataInfo getDataTable(List<?> list)
    {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

    /**
     * 跳转至商品编辑页面
     * @param goodsId
     * @param mmap
     * @return
     */
    @RequestMapping("/edit/{id}")
    public String getEditPage(@PathVariable("id") Integer goodsId, ModelMap mmap){
        //根据id获取商品信息
        TbGoods goods=tbGoodsService.selectTbGoodsById(goodsId);
        String specInfo = goods.getSpecInfo();
        JSONArray specArray = JSONObject.parseArray(specInfo);
        //获取商品图片
        String imgUrl=tbImageService.getImgUrl(goodsId);
        mmap.put("goods",goods);
        mmap.put("specArray",specArray);
        mmap.put("imgUrl",imgUrl);
        return "tbgoods/edit";
    }

    /**
     * 根据goodsId更新商品
     * @param tbGoods
     * @return
     */
    @RequestMapping("/editGoods")
    @ResponseBody
    public AjaxResult editGoods(@RequestBody TbGoods tbGoods){
        AjaxResult result=tbGoodsService.updateGoods(tbGoods);
        return result;
    }

    /**
     * 根据goodsId删除商品
     * @param goodsId
     * @return
     */
    @RequestMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult removeGoodsById(@PathVariable("id")Integer goodsId){
        AjaxResult result=tbGoodsService.removeGoodsById(goodsId);
        return result;
    }
}
